﻿using AnlianCommon.Common.Domains.ProductionManagement;
using AnlianCommon.Common.Domains.SheepFarmHouse;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;

namespace SheepCommon.API.Seedweanings
{
    public class SeedweaningCreateHander : IRequestHandler<SeedweaningCreateCommand, Result>
    {
        private readonly IBaseRepository<Seedweaning> _seedweaningRepository;
        private readonly IMapper _mapper;

        public SeedweaningCreateHander(IBaseRepository<Seedweaning> seedweaningRepository, IMapper mapper)
        {
            _seedweaningRepository = seedweaningRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 种母断奶新增
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(SeedweaningCreateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            try
            {
                var seedweaning = _mapper.Map<Seedweaning>(request);
                seedweaning.CreatedOwnerId = "张三";
                seedweaning.CreatedTime = DateTime.Now;
                var list = await _seedweaningRepository.Add(seedweaning);
                if (list > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                }
                else
                {
                    result.Msg = "添加失败";
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            return result;
        }
    }
}
